home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PACKET / CBBS60SO.ZIP / MBTNC.C < prev    next >
Text File  |  1989-01-26  |  10KB  |  566 lines

  1.  
  2. /*
  3.  *  MBTNC.C - 1/04/89 - Deal with the tnc.
  4.  */
  5.  
  6. #include "mb.h"
  7.  
  8. static char *tcmds[] =
  9. {
  10.   "cono on\n",
  11.   "cono of\n",
  12.   "m on\n",
  13.   "m of\n"
  14. };
  15.  
  16. isdis(cp)
  17. char *cp;
  18. {
  19.   if (search(cp, "*** DISC", 8)) return true;
  20.   return false;
  21. }
  22.  
  23. isreq(cp)
  24. char *cp;
  25. {
  26.   if (matchn(cp, "*** conn", 8)) return true;
  27.   if (matchn(cp, "*** Conn", 8)) return true;
  28.   return false;
  29. }
  30.  
  31. islink(cp)
  32. char *cp;
  33. {
  34.   return matchn(cp, "*** LINK", 8);
  35. }
  36.  
  37. iscon(cp)
  38. char *cp;
  39. {
  40.   return search(cp, "*** CONN", 8);
  41. }
  42.  
  43. isretry(cp)
  44. char *cp;
  45. {
  46.   if (matchn(cp, "*** Retry count exceeded", 24)) return true;
  47.   if (matchn(cp, "*** retry count exceeded", 24)) return true;
  48.   return false;
  49. }
  50. alloff()
  51. {
  52.   allcmd(t_coff);  /* Turn off connects */
  53.   allcmd(t_moff);  /* Turn off monitoring */
  54. }
  55.  
  56. allon()
  57. {
  58.   allcmd(t_mon);  /* Turn on monitoring */
  59.   allcmd(t_con);  /* Turn on connects  */
  60. }
  61.  
  62. login()
  63. {
  64.   register PORTS *p;
  65.   register char *i;
  66.  
  67.   readmsg();
  68.   readusr();
  69.  
  70.   p = port;
  71.  
  72.   p->lport = cport;
  73.   cport->lport = p;
  74.   p->errors = 0;
  75.   p->msg    = NULL;
  76.   p->cmdcnt = 0;
  77.   p->flags clrbit p_opreq;
  78.   p->ec = p->ecuser;
  79.  
  80. /*
  81.  *  Prepare to check if this user can indeed connect.
  82.  */
  83.  
  84.   switch (p->dev)
  85.   {
  86.     case p_console:
  87.       p->mode = local;
  88.       alloff();
  89.       rduser(p->user->call, p->user);
  90.       unbl(p->line, p->user->call, ln_call);
  91.       log('C', 'S', ' ', p->line);
  92.       break;
  93.  
  94.     case p_tnc:
  95.       p->mode = remote;
  96.       tncstate();   /* Pick up the "... connected to ..." line */
  97.       alloff();
  98.       if (p->tmode) trantnc(); else convtnc();
  99. /*
  100.  *  Remove the "; 1 unacked" that pk-232 puts on status line.
  101.  */
  102.       if ((i = strchr(p->line, ';')) isnt NULL) *i = '\0';
  103.       if ((i = strchr(p->line, '*')) isnt NULL) *i = '\0';
  104.       strcpy(p->line, p->line + 28);
  105.  
  106.       logina(false);
  107.       log('C', p->id, ' ', p->line);
  108.       break;
  109.  
  110.     case p_serial:
  111.       p->mode = remote;
  112.       p->flags setbit p_trans;
  113.       alloff();
  114.       strcpy(p->line, p->line + 17);
  115.       logina(false);
  116.       log('C', p->id, ' ', p->line);
  117.       break;
  118.  
  119.     default: ;
  120.   }
  121. }
  122.  
  123. /*
  124.  *  The login checking process.
  125.  */
  126.  
  127. logina(link)
  128. short link;
  129. {
  130.   register char *p, *q;
  131.   register int ssid;
  132.   register short nd;
  133.  
  134.   remnl(port->line);
  135.  
  136. /*
  137.  *  Clear any pending connect request.
  138.  */
  139.  
  140.   port->flags clrbit p_req;
  141.  
  142. /*
  143.  *  Get the users call and ssid.
  144.  */
  145.  
  146.   parse();
  147.   ssid = pcall(tcall, port->fld[0]);
  148.  
  149. /*
  150.  *  Add the call to J list for port, and for connect.
  151.  */
  152.  
  153.   addcall(port->fld[0], cport);
  154.   addcall(port->fld[0], port);
  155.  
  156. /*
  157.  *  Save call of connected gateway, if user linked here.
  158.  */
  159.  
  160.   if (link) strncpy(tmp->scr, port->user->call, ln_call);
  161.  
  162. /*
  163.  *  Get (or make) this users user record.
  164.  */
  165.  
  166.   rduser(tcall, port->user);
  167.  
  168. /*
  169.  *  Update the data in the user record.
  170.  */
  171.  
  172.   port->user->ssid = ssid;
  173.   if (link) port->user->port = 'L'; else port->user->port = port->id;
  174.  
  175. /*
  176.  *  Get path. Count how many digi in path.
  177.  *  First, fix digi path in case is a PK-232.
  178.  */
  179.  
  180.   nd = 3;
  181.   while (port->flds > 3)
  182.   {
  183.     strcat(port->fld[2], port->fld[nd]);
  184.     port->flds--;
  185.     nd++;
  186.   }
  187.  
  188.   nd = 0;
  189.  
  190.   if (link) unbl(port->user->path, tmp->scr, ln_call);
  191.   else if (port->flds > 2)
  192.   {
  193.     nd++;
  194.     p = port->user->path;
  195.     for (q = port->fld[2]; *q; q++)
  196.       if (*q isnt ' ') if ((*p++ = *q) is ',') nd++;
  197.     *p = '\0';
  198.   }
  199.   else *port->user->path = '\0';
  200.  
  201. /*
  202.  *  At last, decide if this user CAN connect.
  203.  *  If sysop or bbs, can always connect.
  204.  */
  205.  
  206.   if (port->user->options & (u_sysop | u_bbs)) {
  207.     outstr("[CBBS-6.0-H$]\n");
  208.     return;
  209.   }
  210.  
  211. /*
  212.  *  If this user is excluded, exclude him.
  213.  */
  214.  
  215.   if (port->user->options & u_exclude) { port->mode = exclude; return; }
  216.  
  217. /*
  218.  *  If this turkey has illegal call, kick him right off.
  219.  */
  220.  
  221.   if (port->priv & p_ilcal) if (!iscall(tcall)) { port->mode = exclude; return; }
  222.  
  223. /*
  224.  *  This user isn't a bbs. If port is bbs only, bye bye.
  225.  */
  226.  
  227.   if (port->priv & p_bbs) { port->mode = exclude; return; }
  228.  
  229. /*
  230.  *  If user connected through too many digi, bye bye.
  231.  */
  232.  
  233.   if (nd > port->ndigi) { port->mode = exclude; return; }
  234.  
  235. /*
  236.  *  Well, this user can indeed connect on this port.
  237.  *  Send our "I am a machine message"
  238.  */
  239.  
  240.   outstr("[CBBS-6.0-H$]\n");
  241.  
  242. /*
  243.  *  If not an expert user, give login message and
  244.  *  tell about unread messages.
  245.  */
  246.  
  247.   if (!(port->user->options & u_expert))
  248.     prtm(motd);
  249.     newmsg();
  250. }
  251.  
  252. /*
  253.  *  Execute a command to each port:
  254.  *  Connects ok, monitor on, etc.
  255.  */
  256.  
  257. allcmd(index)
  258. int index;
  259. {
  260.   register PORTS *p, *was;
  261.   byte ec;
  262.  
  263.   was = port;
  264.   for (p = porthd; p isnt NULL; p = p->next)
  265.   {
  266.     ioport(p);
  267.     if(p->dev is p_tnc)
  268.     {
  269.         p->flags clrbit p_give;
  270.         ec = p->ec;
  271.         p->ec = p->eccmds;
  272.         outstr(tcmds[index]);
  273.         waitcmd(0);
  274.         p->flags setbit p_give;
  275.         p->ec = ec;
  276.     }
  277.   }
  278.   ioport(was);
  279. }
  280.  
  281. alltnc(cp)
  282. char *cp;
  283. {
  284.   register PORTS *p, *was;
  285.   byte ec;
  286.  
  287.   was = port;
  288.   for (p = porthd; p isnt NULL; p = p->next) switch(p->dev)
  289.   {
  290.     case p_tnc:
  291.       ioport(p);
  292.       ec = p->ec;
  293.       p->ec = p->eccmds;
  294.       p->flags clrbit p_give;
  295.       outstr(cp);
  296.       waitcmd(0);
  297.       p->flags setbit p_give;
  298.       p->ec = ec;
  299.       break;
  300.  
  301.     default: ;
  302.   }
  303.   ioport(was);
  304. }
  305.  
  306. /*
  307.  *  Execute a command to the current port:
  308.  *  Connects ok, monitor on, etc.
  309.  */
  310.  
  311. onecmd(index)
  312. int index;
  313. {
  314.   register byte ec;
  315.  
  316.   if(port->dev is p_tnc)
  317.   {
  318.       ec = port->ec;
  319.       port->ec = port->eccmds;
  320.       port->flags clrbit p_give;
  321.       outstr(tcmds[index]);
  322.       waitcmd(0);
  323.       port->ec = ec;
  324.       port->flags setbit p_give;
  325.   }
  326. }
  327.  
  328. onetnc(cp)
  329. char *cp;
  330. {
  331.   register byte ec;
  332.  
  333.   if (port->dev is p_tnc)
  334.   {
  335.     ec = port->ec;
  336.     port->ec = port->eccmds;
  337.     port->flags clrbit p_give;
  338.     outstr(cp);
  339.     waitcmd(0);
  340.     port->ec = ec;
  341.     port->flags setbit p_give;
  342.   }
  343. }
  344.  
  345. /*
  346.  *  Need to distinguish between "disconect in progress", "disconnected",
  347.  *  "connected", "connect in progress".
  348.  */
  349.  
  350. issta(cp)
  351. char *cp;
  352. {
  353.   register char *p;
  354.  
  355.   p = cp;
  356.   if (matchn(cp, "cmd:", 4)) p += 4;
  357.  
  358.   if (!matchn(p, "Link sta", 8)) return false;
  359.  
  360.   if (matchn(p + 15, "DISC", 4) and (strlen(p) < 30)) port->mode = discon;
  361.   return true;
  362. }
  363.  
  364. /*
  365.  *  Put tnc in converse mode.
  366.  */
  367.  
  368. convtnc()
  369. {
  370.   register byte ec;
  371.  
  372.   if(port->dev is p_tnc)
  373.   {
  374.       ec = port->ec;
  375.       port->ec = port->eccmds;
  376.       outstr("conv\n");
  377.       port->ec = ec;
  378.       wait(2);
  379.   }
  380. }
  381.  
  382.  
  383. trantnc()
  384. {
  385.   register byte ec;
  386.  
  387.   if(port->dev is p_tnc)
  388.   {
  389.       ec = port->ec;
  390.       port->ec = port->eccmds;
  391.       outstr("trans\n");
  392.       port->flags setbit p_trans;
  393.       port->ec = ec;
  394.       wait(1);
  395.   }
  396. }
  397.  
  398. /*
  399.  *  Put tnc in command mode.
  400.  */
  401.  
  402. cmdtnc()
  403. {
  404.   register byte ec;
  405.  
  406.   if(port->dev is p_tnc)
  407.   {
  408.       ec = port->ec;
  409.       port->ec = port->eccmds;
  410.       port->flags clrbit p_give;
  411.       if (port->flags & p_trans) breakport(); else outchar(ctl_c);
  412.       waitcmd(1);
  413.       port->ec = ec;
  414.       port->flags setbit p_give;
  415.       port->flags clrbit p_trans;
  416.   }
  417. }
  418.  
  419. /*
  420.  *  Discover the state of the device on the current port.
  421.  */
  422.  
  423. tncstate()
  424. {
  425.   register byte ec;
  426.  
  427.   if (port->mode & idle) return;
  428.  
  429.   if (port->dev is p_tnc)
  430.   {
  431.       ec = port->ec;
  432.       port->ec = port->eccmds;
  433.       cmdtnc();
  434.       port->flags clrbit p_give;
  435.       outstr("C\n");
  436.       while (true)
  437.       {
  438.         getdat();
  439.         if (issta(port->line))
  440.         {
  441.           waitcmd(0);
  442.           port->ec = ec;
  443.           port->flags setbit p_give;
  444.           return;
  445.         }
  446.         else
  447.         {
  448.           cmdtnc();
  449.           port->flags clrbit p_give;
  450.           outstr("C\n");
  451.         }
  452.       }
  453.   }
  454. }
  455.  
  456. /*
  457.  *  Disconnect tnc.
  458.  */
  459.  
  460. distnc()
  461. {
  462.   register int tsave;
  463.   register short done;
  464.   byte ec;
  465.  
  466.   tncstate();
  467.  
  468.   if (port->mode & idle) return;
  469.  
  470.   ec = port->ec;
  471.   port->ec = port->eccmds;
  472.   switch (port->dev)
  473.   {
  474.     case p_serial:
  475.       if (!(port->mode & discon)) outstr("*** DISCONNECTED\n");
  476.       break;
  477.     case p_tnc:
  478.       if (port->mode & discon) break;
  479.       port->flags clrbit p_give;
  480.       outstr("D\n");  /* This does not eat cmd:, thus dtime not used */
  481.       settmr(&port->expire, port->dtime);
  482.       while (true)
  483.       {
  484.         if (instat())
  485.         {
  486.           tsave = port->ctime;
  487.           port->ctime = port->dtime;
  488.           getdat();
  489.           port->ctime = tsave;
  490.           if (isdis(port->line))
  491.           {
  492.             waitcmd(0);
  493.             port->ec = ec;
  494.             port->flags setbit p_give;
  495.             return;
  496.           }
  497.         }
  498.  
  499.         if (!chktmr(port->expire))
  500.         {
  501.           outstr("D\n");
  502.           while (!isdis(port->line)) getdat();
  503.           waitcmd(0);
  504.           port->ec = ec;
  505.           port->flags setbit p_give;
  506.           return;
  507.         }
  508.       }
  509.       break;
  510.   }
  511.   port->ec = ec;
  512.   port->mode = discon;
  513. }
  514.  
  515. /*
  516.  *  Send connect string to tnc.
  517.  */
  518.  
  519. contnc(p)
  520. char *p;
  521. {
  522.   register byte ec;
  523.  
  524.   ec = port->ec;
  525.   port->ec = true;
  526.   switch (port->dev)
  527.   {
  528.     case p_tnc:
  529.       outstr(p);
  530.       port->ec = port->eccmds;
  531.       waitcmd(0);
  532.       break;
  533.  
  534.     case p_serial:
  535.       prtx("*** CONNECTED to $O\n");
  536.   }
  537.   port->ec = ec;
  538. }
  539.  
  540. /*
  541.  *  Wait for "cmd:" prompt.
  542.  */
  543.  
  544. waitcmd(x)
  545. int x;
  546. {
  547.   static char *wanted = "cmd:";
  548.   register short i;
  549.   long l;
  550.   int t;
  551.   if (s_flag & s_dv) t = 5; else t = 2;
  552.   if (x) t = x;
  553.   if(port->dev is p_tnc)
  554.   {
  555.       settmr(&l, t);
  556.       for (i = 0; i < 4;)
  557.       {
  558.         if (instat()) if (inchar() is *(wanted + i)) i++; else i = 0;
  559.         if (!chktmr(l))
  560.           { if (!(port->eccmds))
  561.            bdos(2,tolower(port->id),0); return;}
  562.       }
  563.       if (!(port->eccmds)) bdos(2,port->id,0);
  564.   }
  565. }
  566.